local scene = gamestate.new()

local x = 100
local y = 150
local w = 500
local h = 100
local off = 20

function scene:enter(from,game)
	self.game = game
	local sw,sh = love.graphics.getDimensions()
	x = sw*GlobalScale/2 - w/2
	delay:new(0.1,function()
		net.client:send("need_roomlist")
	end)
	self.checkCD = 1
	self.checkTimer = 0
	self.roomlist = {}
	self.roomID = nil
end

local typeList = {
	hunter = "赏金猎人",
	cross = "穿越火线",
	capture = "基地攻略",
	survive = "最后幸存"}

function scene:addRoom(room)
	local room = {
		text = string.format("游戏类型：%s 创建者：%s\n游戏时间: %d 人数: %d-%d/%d",
			typeList[room.mode],
			room.owner,
			math.floor(room.time),
			room.playerCount,room.min,room.max),
		id = room.id,
	}
	table.insert(self.roomlist,room)
end

function scene:setRoomList(data)
	self.roomlist = {}
    for i ,room in ipairs(data) do
        self:addRoom(room)
    end
end

function scene:update(dt)
	self.checkTimer = self.checkTimer - dt
	love.graphics.setFont(texture.font)
	
	if self.waiting then
		if self.checkTimer<0 then
			self.checkTimer = self.checkCD
			net.client:send("need_room_info",{id = self.roomID})
		end
		suit.Label(self.waiting,x,y-100,w,h)
	else
		if self.roomlist[1] then
			suit.Label("选择房间加入",x,y-100,w,h)
		else
			if self.checkTimer<0 then
				self.checkTimer = self.checkCD
				net.client:send("need_roomlist")
			end
			suit.Label("获取房间列表",x,y-100,w,h)
		end
		
		local offy = 0
		for i = 1,#self.roomlist do
		    local b = self.roomlist[i]
		    local r = suit.Button(b.text,b,x,y+offy,w,h)
		    if r.entered then
		        sound:play("hover")
		    end
		    if r.hit then
		        --gamestate.switch(gameState.netgame,nil,b.id)
		        net.client:send("join_room",{id = b.id})
		    end
			offy = offy + (h + off)
		end
	end
  	self.game:update(dt)
end

function scene:setWaiting(room)
	self.roomID = room.id
	self.waiting = string.format("等待其他玩家(%d-%d/%d)",room.playerCount,room.min,room.max)
end

function scene:draw()
	self.game:draw()
	suit.draw()
end

function scene:keypressed(k)
	if k == "escape" then
		gamestate.switch(gameState.title,self.game)
	end
end

return scene